package com.myres.struts2.action;

import java.io.File;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;

import com.myres.biz.ShopBiz;
import com.myres.model.Shop;
import com.myres.model.User;
import com.myres.spring.SpringUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddShopAction extends ActionSupport {
	private User user;
	private Shop shop;
	private ShopBiz shopBiz;
	private File img;
	private String imgFileName;
    private String imgContentType;
	private String root;
	
	private String name;
	private String address;
	private String scope;
	private String license;
	private java.sql.Time startTime;
	private java.sql.Time endTime;
	
	
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getScope() {
		return scope;
	}
	public void setScope(String scope) {
		this.scope = scope;
	}
	public String getLicense() {
		return license;
	}
	public void setLicense(String license) {
		this.license = license;
	}
	public java.sql.Time getStartTime() {
		return startTime;
	}
	public void setStartTime(java.sql.Time startTime) {
		this.startTime = startTime;
	}
	public java.sql.Time getEndTime() {
		return endTime;
	}
	public void setEndTime(java.sql.Time endTime) {
		this.endTime = endTime;
	}
	public String getRoot() {
		return root;
	}
	public void setRoot(String root) {
		this.root = root;
	}
	public File getImg() {
		return img;
	}
	public void setImg(File img) {
		this.img = img;
	}
	public String getImgFileName() {
		return imgFileName;
	}
	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;
	}
	public String getImgContentType() {
		return imgContentType;
	}
	public void setImgContentType(String imgContentType) {
		this.imgContentType = imgContentType;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Shop getShop() {
		return shop;
	}
	public void setShop(Shop shop) {
		this.shop = shop;
	}
	public ShopBiz getShopBiz() {
		return shopBiz;
	}
	public void setShopBiz(ShopBiz shopBiz) {
		this.shopBiz = shopBiz;
	}
	/**
	 * @return
	 */
	public String execute() {
		// TODO Auto-generated method stub
		user = (User) ActionContext.getContext().getSession().get("user");
		try{
		   saveFile();
		}catch(Exception e){
			e.printStackTrace();
		}
		shop.setName(name);
		shop.setAddress(address);
		shop.setEndTime(endTime);
		shop.setStartTime(startTime);
		shop.setLicense(license);
		shop.setScope(scope);
		shop.setIcon(root);
		shop.setOwner(user);
		shop.setStatus(Shop.SHOP_STATUS_OK);
		shopBiz.insertShop(shop);
		//shopBiz.insertUser(user);
		return SUCCESS;
	}
	private void saveFile() throws IOException{
		root= ServletActionContext.getServletContext().getRealPath("/"); 
		File f=new File(root);
		f=f.getParentFile();
		System.out.println("root="+root);
		// TODO Auto-generated method stub
		  java.io.InputStream is = new java.io.FileInputStream(img);
		  root="/upload/res_pro/"+ imgFileName;
		  String path=f.toString() +"/upload/res_pro/"+ imgFileName;
	      java.io.OutputStream os = new java.io.FileOutputStream(path);
	      byte buffer[] = new byte[8192];
	      int count = 0;
	      while((count = is.read(buffer)) > 0) {
	            os.write(buffer, 0, count);
	      }
	      os.close();
	      is.close();
	}
}